import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.*;

import javax.annotation.Resource;
import java.security.Key;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestCase extends InitAndEnd{

    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("博客编辑","http://localhost:8080/blog_add.html"));
    }

    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "WrongUser.csv")
    void LoginFail(String username,String password){
        webDriver.get("http://localhost:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        webDriver.manage().window().maximize();
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        WebElement element = webDriver.findElement(By.cssSelector(".image"));
        webDriver.findElement(By.cssSelector("#verificationCodeInput")).sendKeys(element.getAttribute("id"));
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        String alert_text = webDriver.switchTo().alert().getText();
        // System.out.println(alert_text);
        Assertions.assertNotEquals("用户登录成功！",alert_text);
        webDriver.switchTo().alert().accept();
    }
    @Order(2)
    @Test
    void MyBlogListFail(){
        String cur_title = webDriver.getTitle();
        Assertions.assertNotEquals("个人博客列表页",cur_title);
    }

    @Order(3)
    @ParameterizedTest
    @CsvSource("huahua,123")
    void LoginSuccess(String username,String password) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        WebElement element = webDriver.findElement(By.cssSelector(".image"));
        webDriver.findElement(By.cssSelector("#verificationCodeInput")).sendKeys(element.getAttribute("id"));
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        String alert_text = webDriver.switchTo().alert().getText();
        // System.out.println(alert_text);
        Assertions.assertEquals("用户登录成功！",alert_text);
        webDriver.switchTo().alert().accept();
    }

    @Order(4)
    @Test
    void MyBlogListSuccess(){
        String cur_tittle = webDriver.getTitle();
        Assertions.assertEquals("个人博客列表页",cur_tittle);
    }

    @Order(5)
    @ParameterizedTest
    @MethodSource("Generator")
    void addBlogButton(String title,String url){
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        String cur_title = webDriver.getTitle();
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(title,cur_title);
        Assertions.assertEquals(url,cur_url);
    }

    @Order(6)
    @Test
    void addBlogFail() throws InterruptedException {
        webDriver.findElement(By.cssSelector("#title")).sendKeys("");
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        webDriver.switchTo().alert().accept();
        sleep(2000);
        webDriver.switchTo().alert().accept();
        sleep(2000);
        String cur_title = webDriver.getTitle();
        System.out.println(cur_title);
        Assertions.assertEquals("博客编辑",cur_title);
    }

    @Order(7)
    @Test
    void addBlogSuccess1(){
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        webDriver.findElement(By.cssSelector("#title")).sendKeys("自动化测试");
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        webDriver.switchTo().alert().accept();
        String cur_tittle = webDriver.getTitle();
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("个人博客列表页",cur_tittle);
        Assertions.assertEquals("http://localhost:8080/myblog_list.html",cur_url);
    }


    @Order(8)
    @Test
    void addBlogSuccess2(){
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        String blog_tittle = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
        int blog_count = webDriver.findElements(By.cssSelector("#artlist > div")).size();
        Assertions.assertEquals(3,blog_count);
        Assertions.assertEquals("自动化测试",blog_tittle);
    }

    @Order(9)
    @Test
    void BlogContentSuccess1(){
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(4)")).click();
        String cur_title = webDriver.getTitle();
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("博客正文",cur_title);
        if(cur_url.contains("http://localhost:8080/blog_content.html?id=")){
            System.out.println("成功跳转至博客详情页！测试通过！");
        }else{
            System.out.println("未成功跳转至博客详情页！测试不通过！");
        }
    }
    @Order(10)
    @ParameterizedTest
    @CsvSource("自动化测试")
    void CheckBlogContent(String blog_title){
        String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
        Assertions.assertEquals(blog_title,cur_blog_title);
    }

    @Order(11)
    @Test
    void CheckBlogReadCount(){
        int readCount = Integer.parseInt(webDriver.findElement(By.cssSelector("#rcount")).getText());
        webDriver.navigate().refresh();
        int cur_readCount = Integer.parseInt(webDriver.findElement(By.cssSelector("#rcount")).getText());
        Assertions.assertEquals(readCount+1,cur_readCount);
    }

    @Order(12)
    @Test
    void updateBlog(){
        webDriver.get("http://localhost:8080/myblog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        String cur_title = webDriver.getTitle();
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("博客编辑",cur_title);
        if(cur_url.contains("http://localhost:8080/blog_edit.html?id=")){
            System.out.println("成功跳转至博客编辑页！测试通过！");
        }else{
            System.out.println("未成功跳转至博客编辑页！测试不通过！");
        }
    }


    @Order(13)
    @ParameterizedTest
    @CsvFileSource(resources = "BlogInfo.csv")
    void CheckUpdateBlog(String blog_title) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        String cur_blog_tittle = webDriver.findElement(By.cssSelector("#title")).getAttribute("value");
        System.out.println(cur_blog_tittle);
        sleep(3000);
        Assertions.assertEquals(blog_title,cur_blog_tittle);
    }

    @Order(14)
    @ParameterizedTest
    @CsvFileSource(resources = "doUpdateBlog.csv")
    void doUpdateBlog(String new_title,String alert_text,String title,String url) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        webDriver.findElement(By.cssSelector("#title")).sendKeys(Keys.CONTROL,"A");
        webDriver.findElement(By.cssSelector("#title")).sendKeys(Keys.CONTROL,"X");
        webDriver.findElement(By.cssSelector("#title")).sendKeys(new_title);
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        sleep(2000);
        String cur_alert_text = webDriver.switchTo().alert().getText();
        Assertions.assertEquals(alert_text,cur_alert_text);
        webDriver.switchTo().alert().accept();
        String cur_title = webDriver.getTitle();
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(title,cur_title);
        Assertions.assertEquals(url,cur_url);
    }

    @Order(15)
    @ParameterizedTest
    @CsvSource("自动化测试修改")
    void CheckUpdateIsSuccess(String blog_title) throws InterruptedException {
        sleep(2000);
        String cur_blog_title = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
        sleep(2000);
        Assertions.assertEquals(blog_title,cur_blog_title);
    }

    @Order(16)
    @Test
    void DeleteBlogFail() throws InterruptedException {
        sleep(2000);
        int blog_count = webDriver.findElements(By.cssSelector("#artlist > div")).size();
        System.out.println(blog_count);
        webDriver.findElement(By.cssSelector("#artlist > div > a:nth-child(6)")).click();
        webDriver.switchTo().alert().dismiss();
        int cur_blog_count = webDriver.findElements(By.cssSelector("#artlist > div")).size();
        Assertions.assertEquals(blog_count,cur_blog_count);
    }

    @Order(17)
    @Test
    void DeleteBlogSuccess() throws InterruptedException {
        sleep(2000);
        webDriver.findElement(By.cssSelector("#artlist > div > a:nth-child(6)")).click();
        webDriver.switchTo().alert().accept();
        sleep(2000);
        String text = webDriver.switchTo().alert().getText();
        sleep(2000);
        webDriver.switchTo().alert().accept();
        Assertions.assertEquals("恭喜：文章删除成功！",text);
    }

    @Order(18)
    @Test
    void logoutFail(){
        String cur_url = webDriver.getCurrentUrl();
        String cur_title = webDriver.getTitle();
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.switchTo().alert().dismiss();
        String new_cur = webDriver.getCurrentUrl();
        String new_title = webDriver.getTitle();
        Assertions.assertEquals(cur_url,new_cur);
        Assertions.assertEquals(cur_title,new_title);
    }

    @Order(19)
    @Test
    void logoutSuccess() throws InterruptedException {
        String cur_url = webDriver.getCurrentUrl();
        String cur_title = webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.switchTo().alert().accept();
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        String new_cur = webDriver.getCurrentUrl();
        String new_title = webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        Assertions.assertNotEquals(cur_url,new_cur);
        Assertions.assertNotEquals(cur_title,new_title);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
    }

}
